﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using ConsolidationTool.WebParts.Layouts.CReport;
using Microsoft.SharePoint;

namespace ConsolidationTool.WebParts.ControlTemplates.ConsolidationTool.WebParts
{
    public partial class VMTreeControl : UserControl
    {
        bool isOpen { get { return Request["control"] == "true"; } }
        public string ctrlType { get { return Request["ctrl"].Split('_')[0];} }
        public List<MasterDataEntity> DataSource;
        public List<string> FirstCheckedValues {

            get
            {
                return GetFirstCheckedValues();
            }
        }

        public List<string> SelectedValues
        {
            get
            {
                return GetSelectedValues();
            }
        }

        private List<string> GetFirstCheckedValues()
        {
            var tmp = new List<string>();
            var nodes = selectBox.Nodes[0].ChildNodes;
            for (int i = 0; i < nodes.Count; i++)
            {
                var node = nodes[i];
                if (node.Checked)
                    tmp.Add(node.Value);
            }
            return tmp;
        }
        private List<string> GetSelectedValues()
        {
            var tmp = new List<string>();
            var nodes = selectBox.CheckedNodes;
            var itemSelectedCount = nodes.Count;
            for (int i = 0; i < itemSelectedCount; i++)
            {
                var node = nodes[i];
                tmp.Add(node.Value);
            }
            return tmp;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadDataSource();
            if (IsPostBack) return;
            filterView.Visible = isOpen;
            if (DataSource == null) return;

            //var rootNodes = DataSource.Select(p => p.Category).Distinct().ToList();
            //rootNodes.ForEach(n =>
            //{

            var rn = new TreeNode(ctrlType, ctrlType);
            rn.SelectAction = TreeNodeSelectAction.Expand;
            RecursiveBinding(rn, "", ctrlType);
            selectBox.Nodes.Add(rn);

            //});
            // selectBox.DataBind();
        }

        private void LoadDataSource()
        {
            if (!isOpen) return;
            var list = SPContext.Current.Web.Lists["MasterData"];
            DataSource = ViewState["_ART_VMMasterDataEntity"] == null ? new List<MasterDataEntity>() : ViewState["_ART_VMMasterDataEntity"] as List<MasterDataEntity>;
            if (DataSource.Count > 0) return;

            var itemCount = list.Items.Count;
            for (int i = 0; i < itemCount; i++)
            {
                DataSource.Add(new MasterDataEntity(list.Items[i]));
            }
            ViewState["_ART_VMMasterDataEntity"] = DataSource;
        }

        private void RecursiveBinding(TreeNode node, string parentCode, string category)
        {
            var dataNodes = DataSource.Where(p => p.ParentCode == parentCode && p.Category == category).ToList();

            dataNodes.ForEach(p =>
            {
                var childNode = new TreeNode()
                 {
                     Text = p.Title,
                     Value = p.Title,
                     ToolTip = p.Description,
                     Checked = true,
                     SelectAction = TreeNodeSelectAction.Expand
                 };
                if (!string.IsNullOrEmpty(p.Description))
                {
                    string temp = p.Title + "-" + p.Description;
                    childNode.Text = temp;
                    childNode.ToolTip = temp;
                }

                RecursiveBinding(childNode, p.Code, category);

                if (node == null)
                    selectBox.Nodes.Add(childNode);
                else
                    node.ChildNodes.Add(childNode);

            });
        }


    }
}
